/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 12/16/2010 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Utility;
using BusinessObject;
using System.Xml;

namespace DataAccessLayer
{
    /// <summary>
    /// Define DonViTinhLoaiHangHoaDao class. This class inherit from  DonViTinhLoaiHangHoaDaoBase class.
    /// If you want add new function, you must define here.
    /// </summary>
    public class DonViTinhLoaiHangHoaDao : DonViTinhLoaiHangHoaDaoBase
    {
        private static ILog logger = null;
        #region Constructor
        /// <summary>
        /// Define Constructor.
        /// </summary>
        public DonViTinhLoaiHangHoaDao() { }
        static DonViTinhLoaiHangHoaDao()
        {
            // Init logger.
            logger = LoggingUtility.GetLogger(typeof(DonViTinhLoaiHangHoaDao).FullName);
        }
        public static bool Insert(List<DonViTinhLoaiHangHoa> lstSource)
        {
            
            String methodName = "InsertDonViTinhLoaiHangHoa";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            ManagerDao.BeginTransaction();
            ///Step 1: Delete all record 
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spDeleteDonViTinhLoaiHangHoa");
            //Add value "MaLoaiHangHoa"  paramater.
            ManagerDao.AddInParameter(cmd, "MaLoaiHangHoa", DbType.Int32,lstSource[0].MaLoaiHangHoa);
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int result = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (result > 0)
                {
                    //Step 2: Insert into DB
                    foreach (DonViTinhLoaiHangHoa objData in lstSource)
                    {
                        if (!Insert(objData))
                        {
                            ManagerDao.Rollback();
                            return false;
                        }
                    }
                    ManagerDao.Commit();
                    return true;
                }                
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);               
            }
            ManagerDao.Rollback();
            return false;
           
        }
        public static bool Insert(DonViTinhLoaiHangHoa obj)
        {
            bool result = false;
            String methodName = "InsertDonViTinhLoaiHangHoa";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertDonViTinhLoaiHangHoa");

            //Add value "MaLoaiHangHoa"  paramater.
            ManagerDao.AddInParameter(cmd, "MaLoaiHangHoa", DbType.Int32, obj.MaLoaiHangHoa);
            //Add value "MaDonViTinh"  paramater.
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32, obj.MaDonViTinh);
          
            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
        public static bool Order(DonViTinhLoaiHangHoa obj, int type)
        {
            bool result = false;
            String methodName = "Order";
            if (logger.IsDebugEnabled)
            {
                LoggingUtility.logMethodEntry(logger, methodName);
            }
            DbCommand cmd = ManagerDao.GetStoredProcCommand("spOrderDonViTinhLoaiHangHoa");

            //Add value "@MaLoaiHangHoa"  paramater.
            ManagerDao.AddInParameter(cmd, "MaLoaiHangHoa", DbType.Int32, obj.MaLoaiHangHoa);
            ManagerDao.AddInParameter(cmd, "MaDonViTinh", DbType.Int32, obj.MaDonViTinh);
            //Add value "SubCategoryId"  paramater.
            ManagerDao.AddInParameter(cmd, "Type", DbType.Int32, type);


            //add paramater store return value.        
            ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
            {
                ManagerDao.ExecuteNonQuery(cmd);
                Int32 id = Int32.Parse(ManagerDao.GetParameterValue(cmd, "returnValue").ToString());

                if (id > 0)//Successfull.
                {
                    result = true;
                }
                else//Error.
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
            }
            return result;
        }
    }
}
        #endregion